package cn.com.dycc.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
 * 时间工具类
 * @author gaofan
 *
 */
public class DateUtil {
	public static String DATE_FMT = "yyyy-MM-dd";
	public static String TIME_FMT = "yyyy-MM-dd HH:mm:ss";
	public static SimpleDateFormat dfForm = null;
	
	/**
	 * 取俩时间的区间内容（包含最大值和最小值）
	 * @param minDate 最小时间
	 * @param maxDate 最大时间
	 * @param format  时间格式
	 * @param content 取值内容
	 * @return
	 */
	public static String[] getDatesBetweenTwoDate(String minDate,String maxDate,String format,String content){
		if(minDate==null||maxDate==null||content==null){
			return null;
		}
		dfForm=new SimpleDateFormat(format);
		try {
			Date beginDate=dfForm.parse(minDate);
			Date endDate=dfForm.parse(maxDate);
			List<Date> lDate = new ArrayList<Date>();
			lDate.add(beginDate);// 把开始时间加入集合
			Calendar cal = Calendar.getInstance();
			// 使用给定的 Date 设置此 Calendar 的时间
			cal.setTime(beginDate);
			boolean bContinue = true;
			while (bContinue) {
				// 根据日历的规则，为给定的日历字段添加或减去指定的时间量
				if("year".equals(content)){
					cal.add(Calendar.YEAR, 1);
				}else if("month".equals(content)){
					cal.add(Calendar.MONTH, 1);
				}else if("day".equals(content)){
					cal.add(Calendar.DAY_OF_MONTH, 1);
				}else {
					return null;
				}				
				// 测试此日期是否在指定日期之后
				if (endDate.after(cal.getTime())) {
					lDate.add(cal.getTime());
				} else {
					break;
				}
			}
			if(endDate.getTime()-beginDate.getTime()>0){
				lDate.add(endDate);// 把结束时间加入集合
			}
			
			String [] date_arrays=new String[lDate.size()];
			//SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
			for(int i=0;i<date_arrays.length;i++){
				date_arrays[i]=dfForm.format(lDate.get(i));
			}
			return date_arrays;

		} catch (ParseException e) {
			e.printStackTrace();
		}

		return null;
	}
	
	/**
	 * 将字符串转换成java.sql.Date类型
	 * @param date
	 * @return
	 */
	public static java.sql.Date getSqlTypeDate(String date){
		if(date==null||date.trim().isEmpty()){
			return null;
		}
		return java.sql.Date.valueOf(date);
	}
	
	/**
	 * 两时间比较大小
	 * @param date1  
	 * @param date2 
	 * @return
	 */
	public static boolean compareToDate(Date date1,Date date2){
		long start=date1.getTime();
		long end=date2.getTime();
		boolean flag=(start-end)>=0? true: false;
		return flag;
	}
	
	/**
	 * 两时间相减
	 * @param date1 格式为yyyy-MM-dd
	 * @param date2 格式为yyyy-MM-dd
	 * @return
	 */
	public static String subDate(String date1,String date2){
		
		dfForm=new SimpleDateFormat(DATE_FMT);		
		try {
			Date time1=dfForm.parse(date1);
			Date time2=dfForm.parse(date2);
			long start=time1.getTime();
			long end=time2.getTime();
			if(start-end<0){
				int day=Long.valueOf((end-start)/(1000*60*60*24)).intValue();
				return StringUtil.format_IntToString(day);
			}else{
				int day=Long.valueOf((start-end)/(1000*60*60*24)).intValue();
				return StringUtil.format_IntToString(day);
			}
			
		} catch (ParseException e) {
			
			e.printStackTrace();
		}
		return null;
		
	}
	/**
	 * 获得日期中的月
	 * @param date
	 * @return
	 */
	public static String getMonth(String date){
		dfForm=new SimpleDateFormat(DATE_FMT);
		Calendar calendar=Calendar.getInstance();
		try {
			Date srcDate=dfForm.parse(date);
			calendar.setTime(srcDate);
			int month=calendar.get(Calendar.MONTH)+1;
			return StringUtil.format_IntToString(month);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "0";
	}
	/**
	 * 获得日期中的年
	 * @param date
	 * @return
	 */
	public static String getYear(String date){
		dfForm=new SimpleDateFormat(DATE_FMT);
		Calendar calendar=Calendar.getInstance();
		try {
			Date srcDate=dfForm.parse(date);
			calendar.setTime(srcDate);
			int year=calendar.get(Calendar.YEAR);
			return StringUtil.format_IntToString(year);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "0";
	}
	/**
	 * 返回当前系统的操作时间
	 * @param format 时间格式
	 * @return
	 */
	public static String getNowDate(String format){
		dfForm=new SimpleDateFormat(format);
		Date now=new Date();
		return dfForm.format(now);
	}
	/**
	 * 返回当前系统时间戳
	 * @return
	 */
	public static Timestamp getNowTimestamp(){
		dfForm=new SimpleDateFormat(TIME_FMT);
		Date now=new Date();
		return Timestamp.valueOf(dfForm.format(now));
		
	}
	/**
	 * 将时间类型转换成字符串
	 * @param date
	 * @param format 转换的格式
	 * @return
	 */
	public static String format_DateToString(Date date,String format){
		dfForm=new SimpleDateFormat(format);
		return dfForm.format(date);
	}
	
	
	/**
	 * 将字符串转换成日期
	 * @param date 时间字符串
	 * @param format 格式
	 * @return
	 */
    public static Date format_StringToDate(String date,String format){
    	dfForm=new SimpleDateFormat(format);
    	try {
			return dfForm.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 将字符串转换成日期
	 * @param str
	 * @return
	 */
    public static Date format_StringToDate(String str){
    	dfForm=new SimpleDateFormat(DATE_FMT);
    	try {
			return dfForm.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
    
    /**
     * 比较时间的大小
     * @param date_b
     * @param date_e
     * @return
     */
    public static boolean equals_Date(Date date_b,Date date_e){
    	return date_b.getTime()-date_e.getTime()>0?true:false;
    }
    
    /**
     * 判断两个时间是否是同一天  linfa
     * @param str 
     * @param format 时间格式
     * @return
     * @throws Exception
     */
    public static boolean isToday(String str1,String str2 ,String format) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date1 = null;
        Date date2 = null;
        try {
            date1 = sdf.parse(str1);
            date2 = sdf.parse(str2);
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
        Calendar c1 = Calendar.getInstance();              
        c1.setTime(date1);                                 
        int year1 = c1.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH)+1;
        int day1 = c1.get(Calendar.DAY_OF_MONTH);     
        Calendar c2 = Calendar.getInstance(); 
        c2.setTime(date2);
        int year2 = c2.get(Calendar.YEAR);
        int month2 = c2.get(Calendar.MONTH)+1;
        int day2 = c2.get(Calendar.DAY_OF_MONTH);   
        if(year1 == year2 && month1 == month2 && day1 == day2){
            return true;
        }
        return false;   
    }
    
    /**
     * 获取某年某月中的天数  linfa
     * @param date
     * @return
     */
    public static int getDaysOfMonth(String str,String format) {
    	SimpleDateFormat sdf = new SimpleDateFormat(format);
    	Date date = null;
    	try {
    		date = sdf.parse(str);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);  
    } 
    
    /**
     * 判断时间是否在时间段内  linfa
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(String nowTime, String beginTime, String endTime) {
    	
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置日期格式
        Date sel_time =null;
        Date b_time = null;
        Date e_time = null;
        try {
        	sel_time = df.parse(nowTime);
        	b_time = df.parse(beginTime);
        	e_time = df.parse(endTime);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return sel_time.getTime() >= b_time.getTime() && sel_time.getTime() <= e_time.getTime(); 
    }
    
    /**
     * 判断两个时间段是否有重合  linfa
     * @param s_beginTime 
     * @param s_endTime
     * @param r_beginTime
     * @param r_endTime
     * @return
     */
    public static boolean coincidenceTime(String s_beginTime,String s_endTime,String r_beginTime,String r_endTime){
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	Date suspendTime = null;
    	Date resumeTime = null;
    	Date leftDate = null;
    	Date rightDate = null;
    	try {
    	  	suspendTime = sdf.parse(s_beginTime);
        	resumeTime = sdf.parse(s_endTime);
        	leftDate= sdf.parse(r_beginTime);
        	rightDate= sdf.parse(r_endTime);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
    	if((suspendTime.getTime() <= leftDate.getTime() || suspendTime.getTime() <rightDate.getTime())
    	&& ((resumeTime.getTime() >leftDate.getTime() || resumeTime.getTime() >= rightDate.getTime()))){
    		return true;
    	}
    	return false;
    }
    
    /**
     * 获取当前时间的前一天 linfa
     * @param date
     * @return
     */
    public static String getLastDay(String time) {
    	String format = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = null;
		try {
			date = sdf.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        calendar.add(Calendar.DAY_OF_MONTH, -1);  
        date = calendar.getTime();
        String last_day = sdf.format(date);
        return last_day;  
    }
    
    /**
     * 比较两个string类型时间大小 linfa
     * @param time1
     * @param time2
     * @param format
     * @return
     */
    public static boolean compare(String time1,String time2,String format){
    	SimpleDateFormat sdf = new SimpleDateFormat(format);
    	boolean flag = false;
    	try {
    		flag=(sdf.parse(time1).getTime()-sdf.parse(time2).getTime())>=0? true: false;
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return flag;
    }
    
//    /**
//     * 判断一个时间是否在一个时间段内，年月日时分秒
//     * @param begin_time
//     * @param end_time
//     * @param compare_time
//     * @return
//     */
//    public static boolean belongTime(String begin_time,String end_time,String compare_time){
//    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//    	Date date1 = null;
//    	Date date2 = null;
//    	Date date3 = null;
//    	try {
//    		date1 = sdf.parse(begin_time);
//    		date2 = sdf.parse(end_time);
//    		date3 = sdf.parse(compare_time);
//		} catch (Exception e) {
//			
//		}
//    	return true;
//    }
    
    
    public static void main(String[] args) throws Exception {
		
//    	String format = "yyyy-MM-dd";
//    	
//    	String str1 = "2018-01-08 08:00:00.0";
//    	String str2 = "2018-01-08 09:00:00.0";
//    	String str3 = "2018-01-08 08:00:00.0";
//    	String str4 = "2018-01-08 09:51:43.0";
//    	
//    	boolean b = DateUtil.coincidenceTime(str3, str4, str1, str2);
//    	System.out.println(b);
//    	
//    	String str2 = "2018-01-07";
//    	
//    	boolean b = DateUtil.isToday(str1, str2, format);
//    	
//    	System.out.println(b);
//    	
//    	int i = DateUtil.getDaysOfMonth(str1, format);
//    	System.out.println(i);
    	
    	String nowTime = "10:32:00";
    	String beginTime = "10:31:00";
    	String endTime = "11:30:00";
    	
    	boolean a = DateUtil.belongCalendar(nowTime, beginTime, endTime);
    	System.out.println(a);
    	
//    	String i = str1.substring(11,19);
//    	System.out.println(i);
    	
    	
    	
    	
    	
	}
	

}
